package es.project.sien7e.main.controllers;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

import es.project.sien7e.main.model.Agenda;
import es.project.sien7e.main.model.Conexion;
import es.project.sien7e.main.model.Contador;
import es.project.sien7e.main.model.Directorio;
import es.project.sien7e.main.model.Foto;
import es.project.sien7e.main.model.Menu;
import es.project.sien7e.main.model.Provincia;
import es.project.sien7e.main.model.Rol;
import es.project.sien7e.main.model.Rolmenu;
import es.project.sien7e.main.model.Tipodirectorio;
import es.project.sien7e.main.model.Usuario;
import es.project.sien7e.main.model.Usuariorol;
import es.project.sien7e.main.services.DAOService;

@Controller
@RequestMapping(value = "ajax")
public class AjaxController {
	private DAOService daoService;
	private Gson gson = new Gson();

	@Autowired
	public void setDAOService(DAOService daoService) {
		this.daoService = daoService;
	}

	@RequestMapping(value = "/usuario/grabar", method = RequestMethod.POST)
	public @ResponseBody
	String grabar(@ModelAttribute("usuario") Usuario usuario,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.grabaUsuario(usuario));
	}

	@RequestMapping(value = "/usuario/insertar", method = RequestMethod.POST)
	public @ResponseBody
	String insertar(@ModelAttribute("usuario") Usuario usuario,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.insertaUsuario(usuario));
	}

	@RequestMapping(value = "/usuario/borrar", method = RequestMethod.POST)
	public @ResponseBody
	String borrarUsuario(@ModelAttribute("usuario") Usuario obj,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.borrarUsuario(obj));
	}

	@RequestMapping(value = "/menu/grabar", method = RequestMethod.POST)
	public @ResponseBody
	String grabar(@ModelAttribute("menu") Menu menu,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.grabaMenu(menu));
	}

	@RequestMapping(value = "/menu/insertar", method = RequestMethod.POST)
	public @ResponseBody
	String insertar(@ModelAttribute("menu") Menu menu,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.insertaMenu(menu));
	}

	@RequestMapping(value = "/menu/borrar", method = RequestMethod.POST)
	public @ResponseBody
	String borrarMenu(@ModelAttribute("menu") Menu obj,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.borrarMenu(obj));
	}

	@RequestMapping(value = "/agenda/grabar", method = RequestMethod.POST)
	public @ResponseBody
	String grabar(@ModelAttribute("agenda") Agenda agenda,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.grabaAgenda(agenda));
	}

	@RequestMapping(value = "/agenda/insertar", method = RequestMethod.POST)
	public @ResponseBody
	String insertar(@ModelAttribute("agenda") Agenda agenda,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.insertaAgenda(agenda));
	}

	@RequestMapping(value = "/agenda/borrar", method = RequestMethod.POST)
	public @ResponseBody
	String borrarAgenda(@ModelAttribute("agenda") Agenda obj,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.borrarAgenda(obj));
	}

	@RequestMapping(value = "/directorio/grabar", method = RequestMethod.POST)
	public @ResponseBody
	String grabar(@ModelAttribute("directorio") Directorio directorio,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.grabaDirectorio(directorio));
	}

	@RequestMapping(value = "/directorio/insertar", method = RequestMethod.POST)
	public @ResponseBody
	String insertar(@ModelAttribute("directorio") Directorio directorio,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.insertaDirectorio(directorio));
	}

	@RequestMapping(value = "/directorio/borrar", method = RequestMethod.POST)
	public @ResponseBody
	String borrarDirectorio(@ModelAttribute("directorio") Directorio obj,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.borrarDirectorio(obj));
	}

	@RequestMapping(value = "/conexion/grabar", method = RequestMethod.POST)
	public @ResponseBody
	String grabar(@ModelAttribute("conexion") Conexion conexion,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.grabaConexion(conexion));
	}

	@RequestMapping(value = "/conexion/insertar", method = RequestMethod.POST)
	public @ResponseBody
	String insertar(@ModelAttribute("conexion") Conexion conexion,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.insertaConexion(conexion));
	}

	@RequestMapping(value = "/conexion/borrar", method = RequestMethod.POST)
	public @ResponseBody
	String borrarConexion(@ModelAttribute("conexion") Conexion obj,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.borrarConexion(obj));
	}

	@RequestMapping(value = "/contador/insertar", method = RequestMethod.POST)
	public @ResponseBody
	String insertar(@ModelAttribute("contador") Contador contador,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.insertaContador(contador));
	}

	@RequestMapping(value = "/tipodirectorio/grabar", method = RequestMethod.POST)
	public @ResponseBody
	String grabar(
			@ModelAttribute("tipodirectorio") Tipodirectorio tipodirectorio,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.grabaTipodirectorio(tipodirectorio));
	}

	@RequestMapping(value = "/tipodirectorio/insertar", method = RequestMethod.POST)
	public @ResponseBody
	String insertar(
			@ModelAttribute("tipodirectorio") Tipodirectorio tipodirectorio,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.insertaTipodirectorio(tipodirectorio));
	}

	@RequestMapping(value = "/tipodirectorio/borrar", method = RequestMethod.POST)
	public @ResponseBody
	String borrarTipodirectorio(
			@ModelAttribute("tipodirectorio") Tipodirectorio obj,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.borrarTipodirectorio(obj));
	}

	@RequestMapping(value = "/rol/grabar", method = RequestMethod.POST)
	public @ResponseBody
	String grabar(@ModelAttribute("rol") Rol rol, HttpServletResponse response)
			throws Exception {
		return gson.toJson(daoService.grabaRol(rol));
	}

	@RequestMapping(value = "/rol/insertar", method = RequestMethod.POST)
	public @ResponseBody
	String insertar(@ModelAttribute("rol") Rol rol, HttpServletResponse response)
			throws Exception {
		return gson.toJson(daoService.insertaRol(rol));
	}

	@RequestMapping(value = "/rol/borrar", method = RequestMethod.POST)
	public @ResponseBody
	String borrarRol(@ModelAttribute("rol") Rol rol,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.borrarRol(rol));
	}

	@RequestMapping(value = "/foto/grabar", method = RequestMethod.POST)
	public @ResponseBody
	String grabar(@ModelAttribute("foto") Foto foto,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.grabaFoto(foto));
	}

	@RequestMapping(value = "/foto/insertar", method = RequestMethod.POST)
	public @ResponseBody
	String insertar(@ModelAttribute("foto") Foto foto,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.insertaFoto(foto));
	}

	@RequestMapping(value = "/foto/borrar", method = RequestMethod.POST)
	public @ResponseBody
	String borrarFoto(@ModelAttribute("foto") Foto foto,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.borrarFoto(foto));
	}

	@RequestMapping(value = "/usuariorol/insertar", method = RequestMethod.POST)
	public @ResponseBody
	String insertar(@ModelAttribute("usuariorol") Usuariorol usuariorol,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.insertaUsuariorol(usuariorol));
	}

	@RequestMapping(value = "/usuariorol/borrar", method = RequestMethod.POST)
	public @ResponseBody
	String borrar(@ModelAttribute("usuariorol") Usuariorol usuariorol,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.borrarUsuariorol(usuariorol));
	}

	@RequestMapping(value = "/rolmenu/insertar", method = RequestMethod.POST)
	public @ResponseBody
	String insertar(@ModelAttribute("rolmenu") Rolmenu rolmenu,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.insertaRolmenu(rolmenu));
	}

	@RequestMapping(value = "/rolmenu/borrar", method = RequestMethod.POST)
	public @ResponseBody
	String borrar(@ModelAttribute("rolmenu") Rolmenu rolmenu,
			HttpServletResponse response) throws Exception {
		return gson.toJson(daoService.borrarRolmenu(rolmenu));
	}

	@RequestMapping(value = "/municipios", method = RequestMethod.GET)
	public @ResponseBody
	String getMunicipios(Long id) throws Exception {
		Provincia provincia = new Provincia();
		provincia.setId(id);
		return gson.toJson(daoService.getMunicipios(provincia));
	}

	@RequestMapping(value = "/menu/padre", method = RequestMethod.GET)
	public @ResponseBody
	String getMenusPadres(Long id) throws Exception {
		return gson.toJson(daoService.getMenusPadres(id));
	}

	@RequestMapping(value = "/menu/principal", method = RequestMethod.GET)
	public @ResponseBody
	String menuPrincipal(Long id) throws Exception {
		return gson.toJson(daoService.getMenuPrincipal(id));
	}

	@RequestMapping(value = "/menu/secundario", method = RequestMethod.GET)
	public @ResponseBody
	String menuSecundario(Long id) throws Exception {
		return gson.toJson(daoService.getMenuSecundario(id));
	}
}
